package com.lhkj.ct.admin.modules.patient.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.google.common.collect.ImmutableMap;
import com.lhkj.ct.base.annntation.Log;
import com.lhkj.ct.base.enums.ActionType;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.validator.group.ValGroups;
import com.lhkj.ct.meta.modules.gauge.service.QrCodeService;
import com.lhkj.ct.meta.modules.patient.converter.PFormConverter;
import com.lhkj.ct.meta.modules.patient.model.entity.TblPatUnit;
import com.lhkj.ct.meta.modules.patient.model.vo.PatUnitForm;
import com.lhkj.ct.meta.modules.patient.service.PatUnitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 * 单位信息表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2023-09-27
 */
@RestController
@RequestMapping("api/plat/patient/unit")
@Api(tags = {"1.2单位管理"})
@Validated
public class PatUnitController {

    @Resource
    private PatUnitService patUnitService;

    @Resource
    private QrCodeService qrCodeService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "单位列表")
    @GetMapping("list")
    //@RequiresPermissions(value = {"system:patUnit:list"}, logical = Logical.OR)
    public ActionResult<List<TblPatUnit>> selectPatUnitTree(TblPatUnit patUnit) {
        return ActionResult.success(patUnitService.selectPatUnitTree(patUnit));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "树级下拉列表")
    @GetMapping("options")
    public ActionResult<List<Option<String>>> listOptions(TblPatUnit patUnit, PageQuery pageQuery) {
        return ActionResult.success(patUnitService.listOptions(patUnit, pageQuery));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "单位表单详情")
    @GetMapping("{unitId}/get")
    //@RequiresPermissions(value = {"system:patUnit:get", "system:patUnit:update"}, logical = Logical.OR)
    public ActionResult<TblPatUnit> all(@PathVariable String unitId) {
        return ActionResult.success(patUnitService.getById(unitId));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增单位")
    @PostMapping("insert")
    @Log(title = "单位管理", actionType = ActionType.INSERT)
    //@RequiresPermissions(value = {"system:patUnit:insert"}, logical = Logical.OR)
    public ActionResult<Void> insert(@Validated(ValGroups.Insert.class) @RequestBody PatUnitForm patUnitForm) {
        TblPatUnit patUnit = PFormConverter.INSTANCE.convertForm2PatUnit(patUnitForm);
        return ActionResult.judge(patUnitService.insert(patUnit));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改单位")
    @PutMapping("update")
    @Log(title = "单位管理", actionType = ActionType.UPDATE)
    //@RequiresPermissions(value = {"system:patUnit:update"}, logical = Logical.OR)
    public ActionResult<Void> update(@Validated(ValGroups.Update.class) @RequestBody PatUnitForm patUnitForm) {
        TblPatUnit patUnit = PFormConverter.INSTANCE.convertForm2PatUnit(patUnitForm);
        return ActionResult.judge(patUnitService.updateById(patUnit));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "删除单位，支持批量")
    @DeleteMapping("delete")
    @Log(title = "单位管理", actionType = ActionType.DELETE)
    //@RequiresPermissions(value = {"system:patUnit:delete"}, logical = Logical.OR)
    public ActionResult<Void> detele(@NotEmpty(message = "标签id不能为空") @RequestBody List<String> ids) {
        return ActionResult.judge(patUnitService.removeByIds(ids));
    }

    @ApiOperationSupport(order = 7)
    @GetMapping(value = "{unitId}/qrCode")
    @ApiOperation(value = "二维码接口")
    public ActionResult<String> getQrCode(@PathVariable String unitId) {
        String url = qrCodeService.generateQrCode("/ct/api/plat/patient/manage/scan/consolidation", ImmutableMap.of("unitId", unitId));
        return ActionResult.success("操作成功", url);
    }
}
